import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
/**
 * 节点遍历，使用 callback 进行处理
 * 如果 callback 返回 true，将会中止遍历
 */
var forEach = function forEach(treeNodes, callback, options) {
  var _ref = options !== null && options !== void 0 ? options : {},
    _ref$level = _ref.level,
    level = _ref$level === void 0 ? 0 : _ref$level,
    _ref$childrenKey = _ref.childrenKey,
    childrenKey = _ref$childrenKey === void 0 ? 'children' : _ref$childrenKey,
    parent = _ref.parent,
    _ref$order = _ref.order,
    order = _ref$order === void 0 ? 'lrd' : _ref$order;
  var breaked;
  var _iterator = _createForOfIteratorHelper(treeNodes),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _treeNode = _step.value;
      var index = treeNodes.indexOf(_treeNode);
      var children = _treeNode[childrenKey];
      var result = void 0;
      if (order == 'dlr') {
        result = callback(_treeNode, {
          level: level,
          index: index,
          parent: parent,
          isMaybeLeaf: !(children !== null && children !== void 0 && children.length)
        });
        if (result) {
          breaked = result;
          break;
        }
      }
      if (children !== null && children !== void 0 && children.length) {
        result = forEach(children, callback, {
          childrenKey: childrenKey,
          level: level + 1,
          parent: _treeNode
        });
        if (result) {
          breaked = result;
          break;
        }
      }
      if (order == 'lrd') {
        result = callback(_treeNode, {
          level: level,
          index: index,
          parent: parent,
          isMaybeLeaf: !(children !== null && children !== void 0 && children.length)
        });
        if (result) {
          breaked = result;
          break;
        }
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return breaked;
};
export default forEach;